<template>
  <div ref="selectRef" class="ch_select" @click="isFocus = true">
    <div class="ch-input ch-input--suffix" :class="{ 'is-focus': isFocus }">
      <div class="ch-input__inner">
        {{ placeholder }}
      </div>
      <input type="hidden" readonly="readonly" v-model="_value" />
      <span class="ch-input__suffix"></span>
      <SelectTree v-show="isFocus" class="fix_select" :treeData="treeData" :valKey="valKey" inSelect />
    </div>
  </div>
</template>

<script>
import { treeEvents } from './tree-events'
import SelectTree from './select-tree'
export default {
  inheritAttrs: false,
  name: 'ChSelect',
  components: { SelectTree },
  props: {
    lazy: Boolean,
    load: Function,
    showCheckbox: Boolean,
    value: null,
    valKey: {
      type: String,
      default: () => 'id'
    },
    labelKey: {
      type: String,
      default: () => 'label'
    },
    treeData: {
      type: Array,
      default: () => [{
        label: '一级 1',
        id: '1',
        children: [{
          id: '2',
          label: '二级 1-1',
          children: [{
            id: '3',
            label: '三级 1-1-1'
          }]
        }]
      }]
    }
  },
  computed: {
    _value: {
      get () {
        return this.value
      },
      set (v) {
        this.$emit('input', v)
      }
    }
  },
  data () {
    return {
      isFocus: false,
      placeholder: '请选择'
    }
  },
  methods: {
    onListen ({ target }) {
      const body = document.querySelector('body')
      let inner = false
      while (target && target.parentNode !== body) {
        if (target.parentNode === this.$refs.selectRef) {
          inner = true
          target = null
        } else {
          target = target.parentNode
        }
      }
      if (!inner) { this.isFocus = false }
    }
  },
  created () {
    window.addEventListener('click', this.onListen)
    treeEvents.subscrible(item => {
      this._value = item[this.valKey]
      this.placeholder = item[this.labelKey]
      this.isFocus = false
    })
  },
  beforeDestroy () {
    window.removeEventListener('click', this.onListen)
    treeEvents.unSubscrible()
  }
}
</script>

<style lang="less">
.ch_select {
  position: relative;
  width: 260px;
  .ch-input__inner {
    appearance: none;
    background-color: rgb(255, 255, 255);
    background-image: none;
    box-sizing: border-box;
    color: rgb(96, 98, 102);
    display: inline-block;
    font-size: inherit;
    height: 40px;
    line-height: 40px;
    width: 100%;
    border-radius: 4px;
    border-width: 1px;
    border-style: solid;
    border-color: rgb(220, 223, 230);
    border-image: initial;
    outline: none;
    padding: 0px 15px;
    transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;
  }
  .is-focus .ch-input__inner {
    border-color: rgb(64, 158, 255);
  }
  .ch-input__suffix {
    position: absolute;
    height: 100%;
    right: 5px;
    top: 0px;
    text-align: center;
    color: rgb(192, 196, 204);
    pointer-events: none;
    transition: all 0.3s ease 0s;
  }
  .fix_select {
    position: absolute;
    padding: 5px;
    left: 0;
    top: 45px;
    min-width: 100%;
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 2px;
    box-shadow: 0 5px 5px #eee;
    &::after {
      content: '';
      width: 0;
      height: 0;
      position: absolute;
      top: -5px;
      left: 5px;
      border-left: 4px solid transparent;
      border-right: 4px solid transparent;
      border-bottom: 4px solid #ddd;
    }
  }
}
</style>
